Skip to content

SectionAutoSpanEventFilter

Base classes

Name Children Inherits
BaseEventFilter
prettyqt.eventfilters.baseeventfilter

⋔ Inheritance diagram

graph TD
  4290306677["eventfilters.SectionAutoSpanEventFilter"]
  2492615352["eventfilters.BaseEventFilter"]
  3251382671["core.Object"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2492615352 --> 4290306677
  3251382671 --> 2492615352
  3227531497 --> 3251382671
  2751680898 --> 3227531497
  3984060476 --> 3251382671
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: BaseEventFilter

Source code in prettyqt/eventfilters/sectionautospaneventfilter.py
class SectionAutoSpanEventFilter(eventfilters.BaseEventFilter):
    ID = "autospan_sections"

    def __init__(
        self,
        parent: widgets.TableView,
        orientation: (
            constants.Orientation | constants.OrientationStr
        ) = constants.HORIZONTAL,
    ):
        super().__init__(parent)
        self._widget = parent
        self._last_start = None
        self._last_end = None
        self.orientation = constants.ORIENTATION.get_enum_value(orientation)
        if self.orientation == constants.HORIZONTAL:
            parent.h_scrollbar.valueChanged.connect(self._update_spans)
        else:
            parent.v_scrollbar.valueChanged.connect(self._update_spans)
        if (model := parent.model()) is not None:
            model.headerDataChanged.connect(self._on_force_update)
        parent.model_changed.connect(self._on_force_update)
        parent.installEventFilter(self)
        self._update_spans()
        # sel_model = parent.selectionModel()
        # if not sel_model:
        #     return

    def _on_force_update(self):
        self._update_spans(True)

    def eventFilter(self, obj, event: core.Event) -> bool:
        match event.type():
            case core.Event.Type.Resize:
                self._update_spans()
                return False
        return super().eventFilter(obj, event)

    def _update_spans(self, force: bool = False):
        if self._widget.model() is None:
            return
        cols = self._widget.get_visible_section_span("horizontal")
        rows = self._widget.get_visible_section_span("vertical")
        start = (rows[0], cols[0])
        end = (rows[1], cols[1])
        if start == self._last_start and end == self._last_end and not force:
            return
        self._last_start = start
        self._last_end = end
        self._widget.clearSpans()
        self._widget.auto_span(orientation=self.orientation, start=start, end=end)

:fa-brands-github: Show source on GitHub